﻿#region License statement
// NJamb, a specification and delta-specification DSL
// Copyright (c) 2010-2011, Mark Knell
// Published under the MIT License; all other rights reserved
#endregion

#region using...
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using NJamb.Specifications;
using Stile.Patterns.SelfDescribingPredicates;
using Stile.Patterns.SelfDescribingPredicates.Evaluators;
#endregion

namespace NJamb.Parser.SpecificationBuilders.SubjectHas.Simple.Enumerable.Quantifiers
{
    public abstract class EnumerableQuantifiedHas<TSubject, TItem, TSpecifies> : IEnumerableQuantifiedHas<TItem, TSpecifies>
        where TSubject : class, IEnumerable<TItem>
        where TSpecifies : class, ISpecification
    {
        protected EnumerableQuantifiedHas(ICollaboratingSpecificationBuilder<TSubject, TSpecifies> specificationBuilder)
        {
            SpecificationBuilder = specificationBuilder;
        }

        public virtual TSpecifies ItemsSatisfying(Expression<Func<TItem, bool>> predicateExpression)
        {
            Evaluator<TSubject, int> evaluator = ExpectEnumerable<TSubject, TItem>.ItemsSatisfying(predicateExpression,
                Count,
                CountPredicate,
                Qualifier);
            TSpecifies specifies = SpecificationBuilder.Specify(evaluator);
            return specifies;
        }

        protected abstract int Count { get; }
        protected abstract Func<int, bool> CountPredicate { get; }
        protected abstract string Qualifier { get; }
        protected ICollaboratingSpecificationBuilder<TSubject, TSpecifies> SpecificationBuilder { get; private set; }
    }
}
